
import javax
import java

import javax.swing.JPanel as JPanel
import javax.swing.event.MouseInputListener as MouseInputListener

class PandoraWhiteboard(JPanel, MouseInputListener):
    def __init__(self):
        javax.swing.JPanel.__init__(self)
        self.drawingList = []
        self.listeners = []
        
        self.addMouseListener(self)
        self.addMouseMotionListener(self)
        
    def mouseClicked(self, me):
        pass
        
    def mouseEntered(self, me):
        pass
        
    def mouseExited(self, me):
        pass
        
    def mousePressed(self, me):
        message = "POINTDOWN|%d|%d" % (me.getX(), me.getY())
        self.sendMessage(message)
        
    def mouseReleased(self, me):
        pass
    
    def mouseDragged(self, me):
        message = "DRAWTO|%d|%d" % (me.getX(), me.getY())
        self.sendMessage(message)
    
    def mouseMoved(self, me):
        pass
    
    def sendMessage(self, me):
        for li in self.listeners:
            li.drawEvent(me)
        
    def addDrawEventListener(self, listener):
        self.listeners.append(listener)
        
    def addDrawEvent(self, me):
        self.drawingList.append(me)
        self.repaint()
        
    def paintComponent(self, g):
        d = self.getSize()
        g.setColor(java.awt.Color(255,255,255))
        g.fillRect(0,0,int(d.getWidth()),int(d.getHeight()))
        g.setColor(java.awt.Color(0,0,0))
        self.originx = 0
        self.originy = 0
        
        for dr in self.drawingList:
            command = dr.split('|')
            if command[0] == 'POINTDOWN':
                self.originx = int(command[1])
                self.originy = int(command[2])
            if command[0] == 'DRAWTO':
                g.drawLine(self.originx, self.originy, int(command[1]), int(command[2]))
                self.originx = int(command[1])
                self.originy = int(command[2])
                

